package com.atguigu.customerdemo.controller;

import com.atguigu.customerdemo.bean.Customer;
import com.atguigu.customerdemo.mapper.CustomerMapper;
import com.atguigu.customerdemo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//@Controller+@ResponseBody=@RestController
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerMapper customerMapper;

    @RequestMapping(value = "byName")
    public String getCustomerByName(@RequestParam("name") String na){
        System.out.println("1111111");
        //调用Service层的方法
        String result = customerService.getCustomerByName(na);
        return result;
    }

    @RequestMapping("byId/{id}")
    public String getCustomerById(@PathVariable("id") String id){
        System.out.println("222222");
        return "Customer:"+id;
    }

//    @RequestMapping(value = "111",method = RequestMethod.POST)
    @PostMapping("save")
    public String SaveCustomer(@RequestBody Customer customer){
//        System.out.println(customer);
        //利用Mybatis-plus中Service层的方法将接收到的参数写入Mysql表中 如果存在则更新，不存在则插入
//        customerService.saveOrUpdate(customer);
        customerMapper.insert(customer);
        return "success";
    }

    @RequestMapping("saveByNameWithAge")
    public void SaveCustomerByNameWithAge(@RequestParam("name") String name,@RequestParam("age") String age){
        Customer customer = new Customer();
        customer.setId("3");
        customer.setName(name);
        customer.setAge(Integer.parseInt(age));


        //利用Mybatis-plus中Mapper层的方法将数据写入Mysql中
        customerMapper.insert(customer);
//        customerMapper.saveCustomerByNameWithAge(name,age);
    }



}
